package com.testfileupload.uploadimg.design.simplefactory.chan.lazy;

import com.testfileupload.uploadimg.design.simplefactory.chan.product.ConcreateProduct1;
import com.testfileupload.uploadimg.design.simplefactory.chan.product.ConcreateProduct2;
import com.testfileupload.uploadimg.design.simplefactory.chan.product.Product;

import java.util.HashMap;
import java.util.Map;

/**
 * 延迟加载工厂类
 */
public class ProductFactory {
    private static final Map<String, Product> premap = new HashMap<>();

    public static synchronized Product createProduct(String type) {
        Product product = null;
        if (premap.containsKey(type)) {
            return premap.get(type);
        } else {
            if ("product1".equals(type)) {
                product = new ConcreateProduct1();
            } else {
                product = new ConcreateProduct2();
            }
            premap.put(type, product);
        }

        return product;
    }
}
